home *** CD-ROM | disk | FTP | other *** search
/ Ghost Rider iactivecard / Ghost Rider iactivecard.iso / pc / resurrection.swf / scripts / __Packages / someFrogs / Puzzles / SlidingPuzzle.as < prev   
Encoding:
Text File  |  2007-04-06  |  14.6 KB  |  505 lines

  1. class someFrogs.Puzzles.SlidingPuzzle extends mx.core.UIComponent
  2. {
  3.    var _picLinkage;
  4.    var _hintStyle;
  5.    var onEnterFrame;
  6.    var piece_width;
  7.    var rez_hrz;
  8.    var piece_height;
  9.    var rez_vrt;
  10.    var emptySpace;
  11.    var boundingBox_mc;
  12.    var puzzlePic_mc;
  13.    var loadListener;
  14.    var controller;
  15.    var border_mc;
  16.    var frogPower_mc;
  17.    var disabled_color;
  18.    static var symbolName = "SlidingPuzzle";
  19.    static var symbolOwner = someFrogs.Puzzles.SlidingPuzzle;
  20.    var className = "SlidingPuzzle";
  21.    var version = "2.0.0.0";
  22.    var _puzzleRezH = 2;
  23.    var _puzzleRezV = 2;
  24.    var autoStart = true;
  25.    var autoMix = true;
  26.    var _puzzleDone = false;
  27.    var _edgesArr = [];
  28.    var useSignature = false;
  29.    var top_depth = 10000;
  30.    var stage_width = 100;
  31.    var stage_height = 100;
  32.    var pic_width = 50;
  33.    var pic_height = 50;
  34.    var mix_cnt = 100;
  35.    var mix_dir = 0;
  36.    var childs_arr = [[]];
  37.    var childs_count = 0;
  38.    function SlidingPuzzle()
  39.    {
  40.       super();
  41.       if(this.autoStart)
  42.       {
  43.          this.start();
  44.       }
  45.    }
  46.    function set picLinkage(picL)
  47.    {
  48.       this._picLinkage = picL;
  49.    }
  50.    function get picLinkage()
  51.    {
  52.       return this._picLinkage;
  53.    }
  54.    function set puzzleRezH(rn)
  55.    {
  56.       this._puzzleRezH = Math.max(Math.floor(rn),2);
  57.       this._puzzleRezH = Math.min(this._puzzleRezH,12);
  58.    }
  59.    function get puzzleRezH()
  60.    {
  61.       return this._puzzleRezH;
  62.    }
  63.    function set puzzleRezV(rn)
  64.    {
  65.       this._puzzleRezV = Math.max(Math.floor(rn),2);
  66.       this._puzzleRezV = Math.min(this._puzzleRezV,12);
  67.    }
  68.    function get puzzleRezV()
  69.    {
  70.       return this._puzzleRezV;
  71.    }
  72.    function set hintStyle(hnSt)
  73.    {
  74.       if(hnSt == "count" || hnSt == "matrix")
  75.       {
  76.          this._hintStyle = hnSt;
  77.       }
  78.       else
  79.       {
  80.          this._hintStyle = "none";
  81.       }
  82.       this.set_hint_style();
  83.    }
  84.    function get hintStyle()
  85.    {
  86.       return this._hintStyle;
  87.    }
  88.    function get puzzleDone()
  89.    {
  90.       return this._puzzleDone;
  91.    }
  92.    function startPuzzle(picLn, hr, vr, auM, hnS)
  93.    {
  94.       if(picLn != undefined)
  95.       {
  96.          this.picLinkage = picLn;
  97.       }
  98.       if(hr != undefined)
  99.       {
  100.          this.puzzleRezH = hr;
  101.       }
  102.       if(vr != undefined)
  103.       {
  104.          this.puzzleRezV = vr;
  105.       }
  106.       if(auM != undefined)
  107.       {
  108.          this.autoMix = auM;
  109.       }
  110.       if(hnS != undefined)
  111.       {
  112.          this.hintStyle = hnS;
  113.       }
  114.       this.clear_stage();
  115.       this.init_picture();
  116.    }
  117.    function start()
  118.    {
  119.       this.clear_stage();
  120.       this.init_picture();
  121.    }
  122.    function mixPuzzle(steps)
  123.    {
  124.       if(steps > 0)
  125.       {
  126.          this.mix_cnt = Number(steps);
  127.       }
  128.       if(this.mix_cnt > 0)
  129.       {
  130.          if(this.onEnterFrame != this.mixPuzzle)
  131.          {
  132.             this.onEnterFrame = this.mixPuzzle;
  133.          }
  134.          else
  135.          {
  136.             this.mix_cnt = this.mix_cnt - 1;
  137.             this.mix_dir = this.mix_dir + 1;
  138.             if(this.mix_dir > 1)
  139.             {
  140.                this.mix_dir = 0;
  141.             }
  142.             var _loc3_ = 2 * this.mix_dir + random(2);
  143.             var _loc6_ = this.moveRandom(_loc3_);
  144.          }
  145.       }
  146.       else
  147.       {
  148.          super.dispatchEvent({type:"mixed",target:this});
  149.          delete this.onEnterFrame;
  150.       }
  151.    }
  152.    function build_puzzle(Void)
  153.    {
  154.       this.init_params();
  155.       this.piece_width = this.pic_width / this.rez_hrz;
  156.       this.piece_height = this.pic_height / this.rez_vrt;
  157.       this.childs_arr = [[]];
  158.       var _loc25_ = (this.stage_width - this.pic_width) / 2;
  159.       var _loc24_ = (this.stage_height - this.pic_height) / 2;
  160.       var _loc9_ = 0;
  161.       var _loc3_ = 0;
  162.       while(_loc3_ < this.rez_vrt)
  163.       {
  164.          this.childs_arr[_loc3_] = [];
  165.          var _loc2_ = 0;
  166.          while(_loc2_ < this.rez_hrz)
  167.          {
  168.             var _loc8_ = _loc2_ * this.piece_width;
  169.             var _loc7_ = _loc3_ * this.piece_height;
  170.             var _loc5_ = {x:_loc8_ + 1,y:_loc7_ + 1,w:this.piece_width - 2,h:this.piece_height - 2,I:_loc2_,J:_loc3_};
  171.             var _loc6_ = {x:_loc25_,y:_loc24_,I:_loc2_,J:_loc3_,active:true,Iorj:_loc2_,Jorj:_loc3_,Hcount:_loc9_,w:this.piece_width,h:this.piece_height};
  172.             var _loc4_ = "pcs_" + _loc3_ + "_" + _loc2_;
  173.             this.createClassObject(someFrogs.Puzzles.SlidingGroup,_loc4_,100 + _loc3_ * this.rez_hrz + _loc2_,{picLinkage:this._picLinkage,edgesArr:this._edgesArr,piecesArr:[_loc5_],groupProps:_loc6_});
  174.             this.childs_arr[_loc3_][_loc2_] = this[_loc4_];
  175.             _loc9_ = _loc9_ + 1;
  176.             this.emptySpace = {I:_loc2_,J:_loc3_};
  177.             _loc2_ = _loc2_ + 1;
  178.          }
  179.          _loc3_ = _loc3_ + 1;
  180.       }
  181.       this.missPiece(true);
  182.       this.boundingBox_mc.useHandCursor = false;
  183.       this.boundingBox_mc.onPress = function()
  184.       {
  185.          this._parent.childPressed();
  186.       };
  187.       if(this.autoMix)
  188.       {
  189.          this.mixPuzzle(5 * this.childs_count);
  190.       }
  191.    }
  192.    function moveRandom(n)
  193.    {
  194.       var _loc3_ = n - 4 * Math.floor(n / 4);
  195.       var _loc2_ = false;
  196.       if(_loc3_ == 0)
  197.       {
  198.          _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J - 1][this.emptySpace.I]) ? this.movePiece(this.childs_arr[this.emptySpace.J + 1][this.emptySpace.I]) : true;
  199.       }
  200.       else if(_loc3_ == 1)
  201.       {
  202.          _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J + 1][this.emptySpace.I]) ? this.movePiece(this.childs_arr[this.emptySpace.J - 1][this.emptySpace.I]) : true;
  203.       }
  204.       else if(_loc3_ == 2)
  205.       {
  206.          _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I - 1]) ? this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I + 1]) : true;
  207.       }
  208.       else if(_loc3_ == 3)
  209.       {
  210.          _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I + 1]) ? this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I - 1]) : true;
  211.       }
  212.       return _loc2_;
  213.    }
  214.    function childPressed(obj)
  215.    {
  216.       if(this.getFocus() != this)
  217.       {
  218.          this.setFocus();
  219.       }
  220.       this.restoreActObj();
  221.       this.promoteActObj(obj);
  222.    }
  223.    function restoreActObj(Void)
  224.    {
  225.       var _loc2_ = this.getInstanceAtDepth(this.top_depth);
  226.       if(_loc2_ != undefined || _loc2_ != null)
  227.       {
  228.          _loc2_.swapDepths(_loc2_.ini_depth);
  229.          _loc2_.activeColor(false);
  230.       }
  231.    }
  232.    function promoteActObj(obj)
  233.    {
  234.       if(obj != undefined || obj != null)
  235.       {
  236.          obj.swapDepths(this.top_depth);
  237.          obj.activeColor(true);
  238.       }
  239.    }
  240.    function childReleased(obj)
  241.    {
  242.       if(this._puzzleDone != true)
  243.       {
  244.          if(this.movePiece(obj))
  245.          {
  246.             super.dispatchEvent({type:"moved",target:this});
  247.             this.see_if_done();
  248.          }
  249.       }
  250.    }
  251.    function see_if_done(Viod)
  252.    {
  253.       if(this._puzzleDone)
  254.       {
  255.          return undefined;
  256.       }
  257.       this._puzzleDone = true;
  258.       var _loc5_ = 0;
  259.       while(_loc5_ < this.rez_vrt)
  260.       {
  261.          var _loc4_ = 0;
  262.          while(_loc4_ < this.rez_hrz)
  263.          {
  264.             var _loc3_ = this.childs_arr[_loc5_][_loc4_];
  265.             if(_loc3_ != null)
  266.             {
  267.                if(_loc3_.groupProps.I != _loc3_.groupProps.Iorj || _loc3_.groupProps.J != _loc3_.groupProps.Jorj)
  268.                {
  269.                   this._puzzleDone = false;
  270.                }
  271.             }
  272.             _loc4_ = _loc4_ + 1;
  273.          }
  274.          _loc5_ = _loc5_ + 1;
  275.       }
  276.       if(this._puzzleDone == true)
  277.       {
  278.          this.missPiece(false);
  279.          super.dispatchEvent({type:"done",target:this});
  280.       }
  281.    }
  282.    function missPiece(bl)
  283.    {
  284.       if(bl == false)
  285.       {
  286.          this.childs_arr[this.emptySpace.J][this.emptySpace.I]._visible = true;
  287.          this.childs_arr[this.emptySpace.J][this.emptySpace.I].active = true;
  288.          this.childs_arr[this.emptySpace.J][this.emptySpace.I].setEnabled(this.enabled);
  289.       }
  290.       else
  291.       {
  292.          this.childs_arr[this.emptySpace.J][this.emptySpace.I]._visible = false;
  293.          this.childs_arr[this.emptySpace.J][this.emptySpace.I].active = false;
  294.          this.childs_arr[this.emptySpace.J][this.emptySpace.I].setEnabled(this.enabled);
  295.       }
  296.    }
  297.    function movePiece(obj)
  298.    {
  299.       var _loc8_ = false;
  300.       if(obj == undefined || obj == null)
  301.       {
  302.          return false;
  303.       }
  304.       if(obj.groupProps.I == undefined || obj.groupProps.I == null)
  305.       {
  306.          return false;
  307.       }
  308.       if(obj.groupProps.J == undefined || obj.groupProps.J == null)
  309.       {
  310.          return false;
  311.       }
  312.       var _loc7_ = Math.abs(obj.groupProps.I - this.emptySpace.I);
  313.       var _loc10_ = Math.abs(obj.groupProps.J - this.emptySpace.J);
  314.       _loc8_ = _loc7_ == 0 && _loc10_ == 1 || _loc7_ == 1 && _loc10_ == 0;
  315.       if(_loc8_)
  316.       {
  317.          var _loc4_ = this.emptySpace.I;
  318.          var _loc3_ = this.emptySpace.J;
  319.          var _loc6_ = obj.groupProps.I;
  320.          var _loc5_ = obj.groupProps.J;
  321.          var _loc9_ = this.childs_arr[_loc3_][_loc4_];
  322.          _loc9_.put_me_to(_loc5_,_loc6_);
  323.          obj.put_me_to(_loc3_,_loc4_);
  324.          this.childs_arr[_loc5_][_loc6_] = _loc9_;
  325.          this.childs_arr[_loc3_][_loc4_] = obj;
  326.          this.emptySpace.I = _loc6_;
  327.          this.emptySpace.J = _loc5_;
  328.       }
  329.       return _loc8_;
  330.    }
  331.    function clear_stage(Void)
  332.    {
  333.       this._puzzleDone = false;
  334.       this.restoreActObj();
  335.       var _loc3_ = 0;
  336.       while(_loc3_ < this.rez_vrt)
  337.       {
  338.          var _loc2_ = 0;
  339.          while(_loc2_ < this.rez_hrz)
  340.          {
  341.             if(typeof this.childs_arr[_loc3_][_loc2_] == "movieclip")
  342.             {
  343.                this.destroyObject(this.childs_arr[_loc3_][_loc2_]._name);
  344.             }
  345.             _loc2_ = _loc2_ + 1;
  346.          }
  347.          _loc3_ = _loc3_ + 1;
  348.       }
  349.    }
  350.    function set_hint_style(Void)
  351.    {
  352.       var _loc3_ = 0;
  353.       while(_loc3_ < this.rez_vrt)
  354.       {
  355.          var _loc2_ = 0;
  356.          while(_loc2_ < this.rez_hrz)
  357.          {
  358.             if(typeof this.childs_arr[_loc3_][_loc2_] == "movieclip")
  359.             {
  360.                this.childs_arr[_loc3_][_loc2_].set_hint_style();
  361.             }
  362.             _loc2_ = _loc2_ + 1;
  363.          }
  364.          _loc3_ = _loc3_ + 1;
  365.       }
  366.    }
  367.    function init_picture(Void)
  368.    {
  369.       if(this.puzzlePic_mc != undefined)
  370.       {
  371.          this.destroyObject("puzzlePic_mc");
  372.       }
  373.       this.createObject(this._picLinkage,"puzzlePic_mc",0);
  374.       if(typeof this.puzzlePic_mc == "movieclip")
  375.       {
  376.          this.set_pic_size(this.puzzlePic_mc._width,this.puzzlePic_mc._height);
  377.          this.build_puzzle();
  378.       }
  379.       else
  380.       {
  381.          this.createEmptyObject("puzzlePic_mc",0);
  382.          var _loc2_ = new MovieClipLoader();
  383.          this.loadListener = new Object();
  384.          this.loadListener.controller = this;
  385.          this.loadListener.onLoadInit = function(target_mc)
  386.          {
  387.             this.controller.set_pic_size(target_mc._width,target_mc._height);
  388.             this.controller.build_puzzle();
  389.          };
  390.          this.loadListener.onLoadError = function(target_mc, errorCode)
  391.          {
  392.             this._picLinkage = "";
  393.             this.controller.set_pic_size();
  394.             this.controller.build_puzzle();
  395.          };
  396.          _loc2_.addListener(this.loadListener);
  397.          _loc2_.loadClip(this._picLinkage,this.puzzlePic_mc);
  398.       }
  399.    }
  400.    function set_pic_size(w, h)
  401.    {
  402.       this.pic_width = w == undefined ? 0.6 * this.stage_width : w;
  403.       this.pic_height = h == undefined ? 0.6 * this.stage_height : h;
  404.       this.puzzlePic_mc._visible = false;
  405.       if(this.pic_width > this.stage_width || this.pic_height > this.stage_height)
  406.       {
  407.          this.setSize(this.pic_width,this.pic_height);
  408.       }
  409.    }
  410.    function init_params(Void)
  411.    {
  412.       if(this.pic_width > this.pic_height)
  413.       {
  414.          this.rez_hrz = Math.max(this._puzzleRezH,this._puzzleRezV);
  415.          this.rez_vrt = Math.min(this._puzzleRezH,this._puzzleRezV);
  416.       }
  417.       else
  418.       {
  419.          this.rez_vrt = Math.max(this._puzzleRezH,this._puzzleRezV);
  420.          this.rez_hrz = Math.min(this._puzzleRezH,this._puzzleRezV);
  421.       }
  422.       this.childs_count = this.rez_vrt * this.rez_hrz;
  423.    }
  424.    function size(Void)
  425.    {
  426.       this.border_mc.move(0,0);
  427.       this.border_mc.setSize(this.stage_width,this.stage_height);
  428.       super.size();
  429.    }
  430.    function init(Void)
  431.    {
  432.       if(this.useSignature && this.frogPower_mc == undefined)
  433.       {
  434.          this.attachMovie("frogPower","frogPower_mc",2);
  435.       }
  436.       this.setSize(this._width,this._height);
  437.       if(this.border_mc == undefined)
  438.       {
  439.          this.createClassObject(_global.styles.rectBorderClass,"border_mc",1,{styleName:this});
  440.       }
  441.       this.border_mc.setSize(this.stage_width,this.stage_height);
  442.       super.init();
  443.       mx.events.EventDispatcher.initialize(this);
  444.       this.setEnabled(this.enabled);
  445.       this.tabChildren = false;
  446.    }
  447.    function setSize(w, h)
  448.    {
  449.       this.stage_width = w;
  450.       this.stage_height = h;
  451.       this._xscale = this._yscale = 100;
  452.       this.boundingBox_mc._alpha = 0;
  453.       this.boundingBox_mc._width = w;
  454.       this.boundingBox_mc._height = h;
  455.       if(this.useSignature)
  456.       {
  457.          this.frogPower_mc._x = this.stage_width / 2 - 40;
  458.          this.frogPower_mc._y = this.stage_height / 2 - 11;
  459.       }
  460.       var _loc3_ = 0;
  461.       while(_loc3_ < this.rez_vrt)
  462.       {
  463.          var _loc2_ = 0;
  464.          while(_loc2_ < this.rez_hrz)
  465.          {
  466.             _loc2_ = _loc2_ + 1;
  467.          }
  468.          _loc3_ = _loc3_ + 1;
  469.       }
  470.       this.size();
  471.    }
  472.    function setEnabled(enabled)
  473.    {
  474.       var _loc5_ = this.getStyle("disabledColor");
  475.       this.disabled_color = {ra:Math.ceil(0.4 * (_loc5_ & 0xFF0000) / 65536),ga:Math.ceil(0.4 * (_loc5_ & 0xFF00) / 256),ba:Math.ceil(0.4 * (_loc5_ & 0xFF))};
  476.       var _loc3_ = 0;
  477.       while(_loc3_ < this.rez_vrt)
  478.       {
  479.          var _loc2_ = 0;
  480.          while(_loc2_ < this.rez_hrz)
  481.          {
  482.             if(typeof this.childs_arr[_loc3_][_loc2_] == "movieclip")
  483.             {
  484.                this.childs_arr[_loc3_][_loc2_].setEnabled(enabled);
  485.             }
  486.             _loc2_ = _loc2_ + 1;
  487.          }
  488.          _loc3_ = _loc3_ + 1;
  489.       }
  490.       this.invalidate();
  491.    }
  492.    function get width()
  493.    {
  494.       return this.stage_width;
  495.    }
  496.    function get height()
  497.    {
  498.       return this.stage_height;
  499.    }
  500.    function onKillFocus(newFocus)
  501.    {
  502.       super.onKillFocus();
  503.    }
  504. }
  505.